﻿using System.Collections.Generic;
using System.Web.Mvc;
using Data;
using Models;

namespace Web.Controllers
{
    public class TagsController : BaseController
    {
        //
        // GET: /Tags/

        public TagsController()
        {
            var dbCategory = new CategoryData();
            ViewBag.NewsCategories = dbCategory.GetCategories();
            var dbTags = new TagsData();
            ViewBag.NewsTags = dbTags.GetTags();
        }

        public ActionResult Index(int? page, string id)
        {
            if (id != null)
            {
                var dbnews = new NewsData();

                var newsList = dbnews.GetNews(tagName:id);

                ViewBag.tagId = id;

                if (newsList != null)
                {
                    var pager = new Pager(newsList.Count, Request.Url.LocalPath, page);
                    if (pager.Check() == null)
                    {
                        Redirect(Request.Url.LocalPath);
                    }
                    ViewBag.pager = pager;
                    return View(newsList);
                }

                return Redirect("/");
            }
            return Redirect("/");
        }

    }
}
